/// @file usbsensor.h
/// @brief Declaration of the Thread class
/// @date 23/09/2009
/// @author Jérôme Stadelmann, Reds, Heig-vd

#ifndef USBSENSOR_H
#define USBSENSOR_H

#include <QThread>
#include "utils/thread.h"
#include "utils/OakHidBase.h"
#include "utils/circularbuffer.h"

using namespace std;
using namespace Toradex;
using namespace Oak;

/// The sampling rate of the sensor
#define SENSOR_PERIOD       0x0a

/// Checks the return value and returns false if failed
#define CHECK_OAK_STATUS(_status)       if (!checkStatus(_status)) return false;

/// Manages the communication with a sensor and puts data into a intermediate buffer
class UsbSensor
        : QThread
{
public:

    /// Constructor
    UsbSensor();

    /// Destructor
    virtual ~UsbSensor();

    /// Initializes the sensor
    /// @param device The name of the USB HID device
    /// @return True if succeeded
    bool initSensor(string device);

    /// Checks if there is a sensor with the specified device name
    /// @param device The name of the USB HID device
    /// @return True if a sensor exists
    static bool checkForSensor(string device);

    /// Start to capture the report data
    /// @return True if succeeded
    bool startCapture();

    /// the run callback used because we are in another thread
    void run();

    /// Checks the status report
    /// @param status The report to check
    /// @return True if succeeded
    bool checkStatus(EOakStatus status);

    /// Stops the capture
    void stop();

    /// Returns an array of data
    /// @param channel The sensors channel
    /// @param array A pointer to the array that will be filled with the sensor data
    /// @param size The maximal size of the array
    /// @param newOnly True for getting only the last values that have not been read since last call
    /// @return The number of values written in the array
    int getBufferArray(int channel, short* array, int size, bool newOnly = false);

private:

    CircularBuffer*     m_pBuffers;

    int                 m_hDevice;
    int                 m_iChannels;
    bool                m_bContinue;
};

#endif // USBSENSOR_H
